package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 刘付磊
 * @date 2021/11/20 0020
 */
@Service
public class MessagesService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private UserApi userApi;

    @Autowired
    private HuanXinTemplate template;

    @DubboReference
    private FriendApi friendApi;

    //根据环信用户id 查询用户信息
    public UserInfoVo userInfoByIdHuanXin(String huanxinid) {
        //根据环信id 查询用户
        User user = userApi.userInfoByIdHuanXin(huanxinid);
        //根据用户id 查询用户详细
        UserInfo userInfo = userInfoApi.findById(user.getId());
        //3.构建vo返回值
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo);
        return vo;
    }

    //添加好友
    public void contacts(Long friendId) {
        //1.将好友关系注册到环信
        Boolean aBoolean = template.addContact(Constants.HX_USER_PREFIX + friendId, Constants.HX_USER_PREFIX + UserHolder.getUserId());
        if (!aBoolean){
            throw new BusinessException(ErrorResult.error());
        }else {
                //2.注册成功 记录好友关系到mongoDB
            friendApi.save(UserHolder.getUserId(),friendId);
        }

    }
    //分页查询联系人列表
    public PageResult findFriends(Integer page, Integer pagesize, String key) {
        //1.调用api查询当前用户的好友数据 -- list<Friend>
        List<Friend> list= friendApi.findByFriends(page,pagesize,UserHolder.getUserId());
        //2.提取表中的好友id
        if(CollUtil.isEmpty(list)){
            return new PageResult();
        }
        List<Long> friendId = CollUtil.getFieldValues(list, "friendId", Long.class);
        //3.调用userInfoAPI查询好友的用户详情
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(key);
        Map<Long, UserInfo> map = userInfoApi.findByIds(friendId, userInfo);
        //4.构造vo对象
        List<ContactVo> vo = new ArrayList<>();
        for (Long aLong : friendId) {
            UserInfo info = map.get(aLong);
            if(info != null){
                ContactVo contactVo = ContactVo.init(info);
                vo.add(contactVo);
            }
        }
        return new PageResult(page,pagesize,0,vo);
    }
}
